package com.vogue.supplymall.order.common;

import com.vogue.gear.memory.provider.IMemProvider;
import com.vogue.supplymall.common.constant.ConstantBean;
import com.vogue.supplymall.common.domain.dto.UserInfo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * The type Order mem utils.
 * 订单、购物车的缓存共通处理类
 *
 * @aouthor: zhangliang
 * @date: Create in 2018-01-17
 */
@Component
public class OrderMemUtils {
    @Resource(name = "memProvider")
    private IMemProvider<Object> memProvider;

    /**
     * Sets cart cache.
     * 设置购物车缓存，并设置缓存时间
     *
     * @param cart         the cart
     * @param cartCount    the cart count
     * @param editCartInfo the edit cart info
     */
    public void setCartCache(Map<String, Object> cart, Integer cartCount, UserInfo editCartInfo) {
        Optional<String> key = getCartKey(editCartInfo, "cart");
        if (key.isPresent()) {
            setCache(cart, cartCount, key.get());
        }
    }

    /**
     * Gets cart cache.
     * 获取购物车缓存
     * 如果缓存超过时间返回null
     *
     * @param editCartInfo the edit cart info
     * @param cls          the cls
     * @return the cart cache
     */
    public Object getCartCache(UserInfo editCartInfo, Class cls) {
        Optional<String> key = getCartKey(editCartInfo, "cart");
        if (key.isPresent()) {
            return getCache(key.get());
        }
        return null;
    }

    private Optional<String> getCartKey(UserInfo editCartInfo, String ident) {
        Long organ = 0L;
        if (null != editCartInfo.getOrganInfo()) {
            organ = editCartInfo.getOrganInfo().getId();
        }
        if (null == editCartInfo.getSyscodeid() || null == editCartInfo.getLoginId()) {
            return Optional.empty();
        }
        return Optional.of(editCartInfo.getSyscodeid() + "-" + organ + "-" + editCartInfo.getLoginId() + "-" + ident);
    }


    /**
     * Sets cart cache.
     * 设置确认订单缓存，并设置缓存时间
     *
     * @param order        the order
     * @param cartCount    the cart count
     * @param editCartInfo the edit cart info
     */
    public void setOrderCache(List<Map<String, Object>> order, Integer cartCount, UserInfo editCartInfo) {
        Optional<String> key = getCartKey(editCartInfo, "order");
        if (key.isPresent()) {
            setCache(order, cartCount, key.get());
        }
    }

    /**
     * Gets cart cache.
     * 获取确认订单缓存
     * 如果缓存超过时间返回null
     *
     * @param editCartInfo the edit cart info
     * @param cls          the cls
     * @return the cart cache
     */
    public Object getOrderCache(UserInfo editCartInfo, Class cls) {
        Optional<String> key = getCartKey(editCartInfo, "order");
        if (key.isPresent()) {
            return getCache(key.get());
        }
        return null;
    }

    private void setCache(Object data, Integer count, String key) {
        Map<String, Object> in = new HashMap<>();
        in.put("count", count);
        in.put("data", data);
        in.put("timer", System.currentTimeMillis());
        memProvider.put(key.toString(), in);
    }

    private Object getCache(String key) {
        Map<String, Object> mem = (Map<String, Object>) memProvider.get(key.toString());
        if (null != mem) {
            Long overTime = Long.valueOf(mem.get("timer").toString()) + ConstantBean.CACHE_EFFECTIVE_TIME;
            if (overTime.compareTo(System.currentTimeMillis()) >= 0) return mem.get("data");
        }
        return mem;
    }
}
